define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user_activity_points_log/index' + location.search,
                    add_url: 'user_activity_points_log/add',
                    edit_url: 'user_activity_points_log/edit',
                    del_url: 'user_activity_points_log/del',
                    multi_url: 'user_activity_points_log/multi',
                    import_url: 'user_activity_points_log/import',
                    table: 'user_activity_points_log',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                $('#total_fees').text(data.total_fees ? data.total_fees : 0)
            });

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data('params', {"custom[group_id][0]":"IN","custom[group_id][1]":["1","2","3"]}).data("field", "username").data('format-item', '{username}/{id}').data("orderBy", "id desc");
                $("input[name='admin_user_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data('params', {"custom[group_id][0]":"IN","custom[group_id][1]":["4"]}).data("field", "username").data('format-item', '{username}/{id}').data("orderBy", "id desc");
                $("input[name='admin_id']", form).addClass("selectpage").data("source", "auth/admin/index").data("primaryKey", "id").data("field", "username").data("orderBy", "id desc");
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'user_id', title: __('User_id'), formatter: function (value, row){return row.user.username;}},
                        {field: 'num', title: __('Num')},
                        {field: 'before', title: __('Before')},
                        {field: 'after', title: __('After')},
                        {field: 'memo', title: __('Memo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'admin_user_id', title: __('Admin_user_id'), formatter: function (value, row){return row.adminuser.username;}},
                        {field: 'admin_id', title: __('Admin_id'), formatter: function (value, row){return row.admin.username;}},
                        // {field: 'admin.username', title: __('Admin.username'), operate: 'LIKE'},
                        // {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
